package service

import (
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"shrimp-blog-server/app/dao"
	"shrimp-blog-server/app/model"
)

func (*NavbarService) AddNavbar(req *ghttp.Request, navbar ...model.Navbar) {
	if len(navbar) == 0 {
		responses.ErrorResponseExit(req, "缺少导航栏信息")
	}
	result, err := dao.Navbar.Insert(navbar...)
	if err != nil {
		g.Log("添加导航栏失败").Error(err)
		panic(gerror.Wrap(err, "添加导航栏失败"))
	}
	if result == 0 {
		responses.ErrorResponseExit(req, "添加导航栏失败")
	}
	responses.SuccessResponse(req)
}

func (*NavbarService) DeleteNavbar(req *ghttp.Request, ids ...uint) {
	if len(ids) == 0 {
		responses.ErrorResponseExit(req, "缺少导航栏ID")
	}
	models := make([]model.Navbar, len(ids))
	for i, id := range ids {
		models[i] = model.Navbar{Id: id, Status: model.NotShowStatus}
	}
	result, err := dao.Navbar.Delete(models...)
	if err != nil {
		g.Log("添加导航栏失败").Error(err)
		panic(gerror.Wrap(err, "添加导航栏失败"))
	}
	if result == 0 {
		responses.ErrorResponseExit(req, "删除导航栏失败")
	}
	responses.SuccessResponse(req)
}

func (*NavbarService) UpdateNavbar(req *ghttp.Request, navbar ...model.Navbar) {
	if len(navbar) == 0 {
		responses.ErrorResponseExit(req, "缺少导航栏信息")
	}
	result, err := dao.Navbar.Update(navbar...)
	if err != nil {
		g.Log("更新导航栏失败").Error(err)
		panic(gerror.Wrap(err, "更新导航栏失败"))
	}
	if result == 0 {
		responses.ErrorResponseExit(req, "更新导航栏失败")
	}
	responses.SuccessResponse(req)
}

func (*NavbarService) GetNavbar(req *ghttp.Request, ids ...uint) {
	var navbarArr []model.Navbar
	var err error
	if len(ids) == 0 {
		navbarArr, err = dao.Navbar.GetModelById(ids...)
	} else {
		err = dao.Navbar.DB().Model(dao.Navbar.Table).Order(dao.Navbar.Columns.Id).Scan(&navbarArr)
	}
	if err != nil {
		g.Log("获取导航栏信息失败").Error(err)
		panic(gerror.Wrap(err, "获取导航栏信息失败"))
	}

	responses.Json(req, successCode, successMessage, navbarArr)
}
